var mvs = require('../network/Matchvs');
var msg = require('../network/MatchvsMessage');
var gameData = require('../network/gameData');
function MatchvsResponse () {

}


MatchvsResponse.prototype.bind = function () {
    mvs.response.initResponse = this.initResponse.bind(this);
    mvs.response.registerUserResponse = this.registerUserResponse.bind(this);
    mvs.response.loginResponse = this.loginResponse.bind(this);
    mvs.response.createRoomResponse = this.createRoomResponse.bind(this);
    mvs.response.getRoomListExResponse = this.getRoomListExResponse.bind(this);
    //mvs.response.getRoomListResponse = this.getRoomListResponse.bind(this);
    mvs.response.joinRoomResponse = this.joinRoomResponse.bind(this);
    mvs.response.joinRoomNotify = this.joinRoomNotify.bind(this);
    mvs.response.sendEventResponse = this.sendEventResponse.bind(this);
    mvs.response.sendEventNotify = this.sendEventNotify.bind(this);
    mvs.response.leaveRoomResponse = this.leaveRoomResponse.bind(this);
    mvs.response.leaveRoomNotify= this.leaveRoomNotify.bind(this);
}

MatchvsResponse.prototype.init = function (Context) {
    this.context = Context;
}



MatchvsResponse.prototype.initResponse = function (status) {
    this.context.node.emit(msg.MATCHVS_INITRESPONSE,status);
}

MatchvsResponse.prototype.registerUserResponse = function (userInfo) {
    this.context.node.emit(msg.MATCHVS_REGISTERUSERRESPONSE,userInfo);
}

MatchvsResponse.prototype.loginResponse = function (MsLoginRsp) {
    this.context.node.emit(msg.MATHCVS_LOGINRESPONSE,MsLoginRsp);
}

// MatchvsResponse.prototype.mainBind = function () {
//     mvs.response.createRoomResponse = this.createRoomResponse.bind(this);
// }

MatchvsResponse.prototype.mainInit = function (Context) {
    this.mContext = Context;
}

MatchvsResponse.prototype.createRoomResponse = function (createRoomRsp) {
    console.log(createRoomRsp);
    //这个监听为什么会报错 emit无效
    //，即使换个脚本也同样会报错， 换脚本第一次可以，但第二次出错
    //错误原因 在create()时就进行了场景切换，然而， createRoomResponse还在原来的场景中 然后this.mContext 就失效了
    this.mContext.node.emit(msg.MATCHVS_CREATEROOMRESPONOSE,createRoomRsp);
}


// MatchvsResponse.prototype.getRoomListResponse = function (status,roomInfos) {
//     console.log(status);
//     console.log(roomInfos);
// }

MatchvsResponse.prototype.getRoomListExResponse = function (rsp) {
    console.log(rsp);
    this.mContext.node.emit(msg.MATCHVS_GETROOMLISTEXRESPONSE,rsp);
}

MatchvsResponse.prototype.fightInit = function (Context) {
    this.fContext = Context;
}

MatchvsResponse.prototype.joinRoomResponse = function (status, roomUserInfoList,roomInfo) {
    gameData.roomInfo.roomID = roomInfo.roomID;
    console.log(status);
    if(status == 200) {
        //cc.director.loadScene("fight");
    }
    //在roomUserInfoList 中通过userProfile 来得到加入房间者的名字，在通过索引名字得到加入者的所有信息
    console.log(roomUserInfoList);
    //在官方文档里 roomInfo 里面没有roomName参数，但是实际中存在，不过得不到房间名
    console.log(roomInfo);
    this.fContext.node.emit(msg.MATCHVS_JOINROOMRESPONSE,roomUserInfoList,roomInfo);
    
}

MatchvsResponse.prototype.joinRoomNotify = function (roomUserInfo) {
    console.log(roomUserInfo);
    //cc.find('scenes/fight').emit(msg.MATCHVS_JOINROOMNOTIFY,roomUserInfo);
    this.fContext.node.emit(msg.MATCHVS_JOINROOMNOTIFY,roomUserInfo);
}

MatchvsResponse.prototype.sendEventResponse = function (rsp) {
    this.fContext.node.emit(msg.MATCHVS_SENDEVENTRESPONSE,rsp);
}

MatchvsResponse.prototype.sendEventNotify = function (eventInfo) {
    this.fContext.node.emit(msg.MATCHVS_SENDEVENTNOTIFY,eventInfo);
}

MatchvsResponse.prototype.leaveRoomResponse = function (rsp) {
    this.fContext.node.emit(msg.MATCHVS_LEAVEROOMRESPONSE, rsp);
}

MatchvsResponse.prototype.leaveRoomNotify = function (leaveRoomInfo) {
    this.fContext.node.emit(msg.MATCHVS_LEAVEROOMNOTIFY, leaveRoomInfo);
}


module.exports = MatchvsResponse;

